global simulinkFileName;
simulinkModeName="sanxiangsanxian_simpleV5";
simulinkFileName=simulinkModeName+"/";
userID="alluser";



%(数据生成数量)1000条数据大概100分钟,2000条用03:06:07，4000条用06:49:22
generateDataAmount=5000; 



%用户PQ数据文件名路径
dayPowerDataFilePath="D:\Python\Workspace\ImportantData\NewData\fix日负荷PQ提取结果\PQ_data_all34.xlsx";
%输出数据文件名,使用默认路径
dt=datetime;
dtstr=datestr(dt,'yyyymmddHHMMSS');
outdata_filename=userID+"-"+dtstr+"-outdata.csv";
outdatainfo_filename=userID+"-"+dtstr+"-outdatainfo.csv";

%程序开始时间
startTime=datetime;

%输入输出数据大小预设值,提高运行速度
foutdata=zeros(generateDataAmount, 960);
foutdatainfo=string(zeros(generateDataAmount, 4));

%读取Excel文件
dayPowerData=readtable(dayPowerDataFilePath,'ReadRowNames',true,'ReadVariableNames',true,"VariableNamingRule","preserve");

%电流互感器变比
defaultCTRatio=10;

%电压互感器变比
defaultVTRatio=100;

%数据生成循环开始:
for excelDataNum=1:generateDataAmount
%设置负荷曲线
    
    %获取Excel的行数
    PQ_count=height(dayPowerData)/2;
    %随机一个PQ数据行数,取得Q行的序号
    PQ_data_num=unidrnd(PQ_count)*2;
    %利用Q行的序号找到索引的名称
    PQ_date_char_array=dayPowerData.PQinfo{PQ_data_num};
    %索引的名称去掉后两位编程PQ数据的日期
    date_char_array=PQ_date_char_array(1:end-2);
    %将日期字符数组转换为字符串
    date_str=string(date_char_array);
    %利用日期拼接字符串得到PQ数据的索引,利用索引取出PQ数据
    PQ_data=dayPowerData([date_str+"-P",date_str+"-Q"],:);
    %将table类型的PQ数据转换为矩阵
    PQ_data_array=table2array(PQ_data);
    
    %动态负荷曲线
    dynamicload_P_temp=PQ_data_array(1,:);
    dynamicload_Q_temp=PQ_data_array(2,:);
    %优化动态负荷曲线
    dynamicload_P=[0,0,0,0,dynamicload_P_temp];
    dynamicload_Q=[0,0,0,0,dynamicload_Q_temp];
    
    %设置动态负荷曲线
    PQ_ratio=defaultCTRatio*defaultVTRatio;
    set_param(simulinkFileName+"PData",'OutValues',mat2str(dynamicload_P*PQ_ratio));
    set_param(simulinkFileName+"QData",'OutValues',mat2str(dynamicload_Q*PQ_ratio));
    
    BreakerSignalNames=["VCT_C_Pha_SC_Signal","VCT_C_Phc_SC_Signal","PM_C_Pha_RE_Signal","PM_C_Phc_RE_Signal"...
                ,"VCT_V_Pha_Ls_Signal","VCT_V_Phb_Ls_Signal","VCT_V_Phc_Ls_Signal","VCT_V_Phab_Re_Signal"...
                ,"VCT_V_Phbc_Re_Signal","VCT_V_Phca_Re_Signal","PM_MC_CTRatio_Signal","PM_MC_VTRatio_Signal"...
                ,"VCT_C_PhAandC_ST_Signal","VCT_V_PhAandC_ST_Signal","PM_C_PhAandC_ST_Signal"];
    
    
    %窃电参数,第一阶段窃电8:00-18:00,总共40个点，第二阶段为20:00-7:30总共47个点
    [InitialTime,FinalTime]=NewGenerateETTime(unidrnd(11));

    
    %改电流互感器变比窃电

    PM_MCCTRatioFake=unidrnd(defaultCTRatio/1.25);
    
    %改电压互感器变比窃电

    PM_MCVTRatioFake=unidrnd(8)*10;
    
    %互感器饱和窃电
    tfSaturationRate=80+unidrnd(19);
    
    %窃电类型
    ETType=unidrnd(16)-1;
    %ETType=0;
    onIndex= ETType ;
    offIndex= ETType ;
    
    %复位所有开关
    ResetAllBreakerSingal(defaultCTRatio,defaultVTRatio);
    
    %打开窃电开关
    switch(onIndex)
        case 0
            disp("ON is 0,No ETtype set to Simulink")
        case 1
            VCT_C_Pha_SC_ON(InitialTime,FinalTime)
            disp("A相电流短接,VCT_C_Pha_SC_ON set to Simulink")
        case 2
            VCT_C_Phc_SC_ON(InitialTime,FinalTime)
            disp("C相电流短接,VCT_C_Phc_SC_ON set to Simulink")
        case 3
            PM_C_Pha_RE_ON(InitialTime,FinalTime)
            disp("A相电流反向,PM_C_Pha_RE_ON set to Simulink")
        case 4
            PM_C_Phc_RE_ON(InitialTime,FinalTime)
            disp("C相电流反向,PM_C_Phc_RE_ON set to Simulink")
        case 5
            VCT_V_Pha_Ls_ON(InitialTime,FinalTime)
            disp("A相失压,VCT_V_Pha_Ls_ON set to Simulink")
        case 6
            VCT_V_Phb_Ls_ON(InitialTime,FinalTime)
            disp("B相失压,VCT_V_Phb_Ls_ON set to Simulink")
        case 7
            VCT_V_Phc_Ls_ON(InitialTime,FinalTime)
            disp("C相失压,VCT_V_Phc_Ls_ON set to Simulink")
        case 8
            VCT_V_Phab_Re_ON(InitialTime,FinalTime)
            disp("AB两相错接,VCT_V_Phab_Re_ON set to Simulink")
        case 9
            VCT_V_Phbc_Re_ON(InitialTime,FinalTime)
            disp("BC两相错接,VCT_V_Phbc_Re_ON set to Simulink")
        case 10
            VCT_V_Phca_Re_ON(InitialTime,FinalTime)
            disp("CA两相错接,VCT_V_Phca_Re_ON set to Simulink")
        case 11
            PM_MC_CTRatio_ON(InitialTime,FinalTime,defaultCTRatio,PM_MCCTRatioFake)
            disp("电流互感器变比改变,PM_MC_CTRatio_ON set to Simulink")
        case 12
            PM_MC_VTRatio_ON(InitialTime,FinalTime,defaultVTRatio,PM_MCVTRatioFake)
            disp("电压互感器变比改变,PM_MC_VTRatio_ON set to Simulink")
        case 13
            PM_C_PhAandC_ST_ON(InitialTime,FinalTime,tfSaturationRate)
            disp("电能表电流互感器饱和,PM_C_PhAandC_ST_ON set to Simulink")
        case 14
            VCT_V_PhAandC_ST_ON(InitialTime,FinalTime,tfSaturationRate)
            disp("配电侧电压互感器饱和,VCT_V_PhAandC_ST_ON set to Simulink")
        otherwise
            disp("没有相应的窃电方式,打开失败")
    end
    
    simout = sim(simulinkModeName);
    
    foutdata(excelDataNum,:)=NewFormatOutData(simout);
    foutdatainfo(excelDataNum,:)=[date_str,InitialTime,FinalTime,ETType];
    
    %关闭窃电开关
    switch(offIndex)
        case 0
            disp("OFF is 0,No ETtype set to Simulink")
        case 1
            VCT_C_Pha_SC_OFF()
        case 2
            VCT_C_Phc_SC_OFF()
        case 3
            PM_C_Pha_RE_OFF()
        case 4
            PM_C_Phc_RE_OFF()
        case 5
            VCT_V_Pha_Ls_OFF()
        case 6
            VCT_V_Phb_Ls_OFF()
        case 7
            VCT_V_Phc_Ls_OFF()
        case 8
            VCT_V_Phab_Re_OFF()
        case 9
            VCT_V_Phbc_Re_OFF()
        case 10
            VCT_V_Phca_Re_OFF()
        case 11
            PM_MC_CTRatio_OFF(defaultCTRatio,defaultCTRatio)
        case 12
            PM_MC_VTRatio_OFF(defaultVTRatio,defaultVTRatio)
        case 15
            PM_C_PhAandC_ST_OFF()
        case 14
            VCT_V_PhAandC_ST_OFF()

        otherwise
            disp("没有相应的窃电方式,关闭失败")
    end

%for循环的end
disp(num2str(excelDataNum)+"/"+num2str(generateDataAmount)+":"+date_str);
end

%保存将数据集保存为文件
%xlswrite(outfilename,foutdata,1);
%xlswrite(outfilename,foutdatainfo,2);
writematrix(foutdata,outdata_filename)
writematrix(foutdatainfo,outdatainfo_filename)

endTime=datetime;
disp("总共耗时：")
disp(endTime-startTime);
disp("Work Done!")

%电流互感器短接窃电

function VCT_C_Pha_SC_ON(InitialTime,FinalTime)
    global simulinkFileName;
    signalname="VCT_C_Pha_SC_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function VCT_C_Pha_SC_OFF()
    global simulinkFileName;
    signalname="VCT_C_Pha_SC_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
end

function VCT_C_Phc_SC_ON(InitialTime,FinalTime)
    global simulinkFileName;
    signalname="VCT_C_Phc_SC_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function VCT_C_Phc_SC_OFF()
    global simulinkFileName;
    signalname="VCT_C_Phc_SC_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
end

%电流互感器反接线窃电
function PM_C_Pha_RE_ON(InitialTime,FinalTime)
    global simulinkFileName;
    signalname="PM_C_Pha_RE_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function PM_C_Pha_RE_OFF()
    global simulinkFileName;
    signalname="PM_C_Pha_RE_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
end

function PM_C_Phc_RE_ON(InitialTime,FinalTime)
    global simulinkFileName;
    signalname="PM_C_Phc_RE_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function PM_C_Phc_RE_OFF()
    global simulinkFileName;
    signalname="PM_C_Phc_RE_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
end

%电压互感器断相窃电
function VCT_V_Pha_Ls_ON(InitialTime,FinalTime)
    global simulinkFileName;
    signalname="VCT_V_Pha_Ls_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function VCT_V_Pha_Ls_OFF()
    global simulinkFileName;
    signalname="VCT_V_Pha_Ls_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
end

function VCT_V_Phb_Ls_ON(InitialTime,FinalTime)
    global simulinkFileName;
    signalname="VCT_V_Phb_Ls_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function VCT_V_Phb_Ls_OFF()
    global simulinkFileName;
    signalname="VCT_V_Phb_Ls_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
end

function VCT_V_Phc_Ls_ON(InitialTime,FinalTime)
    global simulinkFileName;
    signalname="VCT_V_Phc_Ls_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function VCT_V_Phc_Ls_OFF()
    global simulinkFileName;
    signalname="VCT_V_Phc_Ls_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
end

%电压互感器换相窃电
function VCT_V_Phab_Re_ON(InitialTime,FinalTime)
    global simulinkFileName;
    signalname="VCT_V_Phab_Re_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function VCT_V_Phab_Re_OFF()
    global simulinkFileName;
    signalname="VCT_V_Phab_Re_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
end

function VCT_V_Phbc_Re_ON(InitialTime,FinalTime)
    global simulinkFileName;
    signalname="VCT_V_Phbc_Re_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function VCT_V_Phbc_Re_OFF()
    global simulinkFileName;
    signalname="VCT_V_Phbc_Re_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
end

function VCT_V_Phca_Re_ON(InitialTime,FinalTime)
    global simulinkFileName;
    signalname="VCT_V_Phca_Re_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function VCT_V_Phca_Re_OFF()
    global simulinkFileName;
    signalname="VCT_V_Phca_Re_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
end

%改电流互感器变比窃电
function PM_MC_CTRatio_ON(InitialTime,FinalTime,CTRatio,PM_MCCTRatio)
    global simulinkFileName;
    signalname="PM_MC_CTRatio_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
    setCtRatioParameter(simulinkFileName,CTRatio,CTRatio,PM_MCCTRatio);
end

function PM_MC_CTRatio_OFF(CTRatio,PM_MCCTRatio)
    global simulinkFileName;
    signalname="PM_MC_CTRatio_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
    setCtRatioParameter(simulinkFileName,CTRatio,CTRatio,PM_MCCTRatio);
end

%改电压互感器变比窃电
function PM_MC_VTRatio_ON(InitialTime,FinalTime,VTRatio,PM_MCVTRatio)
    global simulinkFileName;
    signalname="PM_MC_VTRatio_Signal";
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
    setVtRatioParameter(simulinkFileName,VTRatio,VTRatio,PM_MCVTRatio);
end

function PM_MC_VTRatio_OFF(VTRatio,PM_MCVTRatio)
    global simulinkFileName;
    signalname="PM_MC_VTRatio_Signal";
    SetBreakerSignalDown(simulinkFileName,signalname)
    setVtRatioParameter(simulinkFileName,VTRatio,VTRatio,PM_MCVTRatio);
end

%电流互感器饱和窃电
function VCT_C_PhAandC_ST_ON(InitialTime,FinalTime,tfSaturationRate)
    global simulinkFileName;
    signalname="VCT_C_PhAandC_ST_Signal";
    VCTname="VCT_CT";
    SetVCTSaturationParameter(simulinkFileName,VCTname,tfSaturationRate)
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function VCT_C_PhAandC_ST_OFF()
    global simulinkFileName;
    signalname="VCT_C_PhAandC_ST_Signal";
    VCTname="VCT_CT";
    SetVCTSaturationParameter(simulinkFileName,VCTname,1)
    SetBreakerSignalDown(simulinkFileName,signalname)
    
end

%电压互感器饱和窃电
function VCT_V_PhAandC_ST_ON(InitialTime,FinalTime,tfSaturationRate)
    global simulinkFileName;
    VCTname="VCT_VT";
    signalname="VCT_V_PhAandC_ST_Signal";
    SetVCTSaturationParameter(simulinkFileName,VCTname,tfSaturationRate)
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function VCT_V_PhAandC_ST_OFF()
    global simulinkFileName;
    VCTname="VCT_VT";
    signalname="VCT_V_PhAandC_ST_Signal";
    SetVCTSaturationParameter(simulinkFileName,VCTname,1)
    SetBreakerSignalDown(simulinkFileName,signalname)
end

%电能表电流互感器饱和窃电
function PM_C_PhAandC_ST_ON(InitialTime,FinalTime,tfSaturationRate)
    global simulinkFileName;
    VCTname="PM_CT";
    signalname="PM_C_PhAandC_ST_Signal";
    SetVCTSaturationParameter(simulinkFileName,VCTname,tfSaturationRate)
    SetBreakerSignalUp(simulinkFileName,signalname,InitialTime,FinalTime)
end

function PM_C_PhAandC_ST_OFF()
    global simulinkFileName;
    VCTname="PM_CT";
    signalname="PM_C_PhAandC_ST_Signal";
    SetVCTSaturationParameter(simulinkFileName,VCTname,1)
    SetBreakerSignalDown(simulinkFileName,signalname)
    
end

%复位开关信号
function ResetAllBreakerSingal(defaultCTRatio,defaultVTRatio)
    
    VCT_C_Pha_SC_OFF()
    VCT_C_Phc_SC_OFF()
    PM_C_Pha_RE_OFF()
    PM_C_Phc_RE_OFF()
    VCT_V_Pha_Ls_OFF()
    VCT_V_Phb_Ls_OFF()
    VCT_V_Phc_Ls_OFF()
    VCT_V_Phab_Re_OFF()
    VCT_V_Phbc_Re_OFF()
    VCT_V_Phca_Re_OFF()
    PM_MC_CTRatio_OFF(defaultCTRatio,defaultCTRatio)
    PM_MC_VTRatio_OFF(defaultVTRatio,defaultVTRatio)
    PM_C_PhAandC_ST_OFF()
    VCT_V_PhAandC_ST_OFF()
end